﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

/// <summary>
/// FileHelper 的摘要说明
/// </summary>
public class FileHelper
{
    HttpContext context;

    public FileHelper(HttpContext context)
    {
        this.context = context;
    }

    public void burnReceiveDirectory()
    {
        try
        {
            JToken receiveConf = (JToken)JsonConvert.DeserializeObject(File.ReadAllText(context.Server.MapPath("../Conf/FileReceiveConf.json")));
            string receiveZipFileDirect = context.Server.MapPath("~/" + receiveConf["receivePackZipPath"].ToString());
            int receiveZipSavingDays = Convert.ToInt32(receiveConf["zipSavingDays"]);
            string[] receiveDirects = Directory.GetDirectories(receiveZipFileDirect);
            foreach (string direct in receiveDirects)
            {
                if (Directory.Exists(direct) && Directory.GetCreationTime(direct) < DateTime.Now.AddDays(0 - receiveZipSavingDays))
                {
                    Directory.Delete(direct, true);
                }
            }
        }
        catch (Exception ex)
        {
            MyLog mylog = new MyLog(context);
            mylog.writeLog(ex.Message);
        }

    }

    public void burnSendDirectory()
    {
        try
        {
            JToken sendConf = (JToken)JsonConvert.DeserializeObject(File.ReadAllText(context.Server.MapPath("../Conf/FileSenderConf.json")));
            string sendZipFileDirect = context.Server.MapPath("~/" + sendConf["sendingPackZipPath"].ToString());
            int sendZipSavingDays = Convert.ToInt32(sendConf["zipSavingDays"]);
            string[] sendDirects = Directory.GetDirectories(sendZipFileDirect);
            foreach (string direct in sendDirects)
            {
                if (Directory.Exists(direct) && Directory.GetCreationTime(direct) < DateTime.Now.AddDays(0 - sendZipSavingDays))
                {
                    Directory.Delete(direct, true);
                }
            }
        }
        catch (Exception ex)
        {
            MyLog mylog = new MyLog(context);
            mylog.writeLog(ex.Message);
        }


    }
}